extends CanvasLayer

const HEART_ROW_SIZE = 8
const HEART_OFFSET = 16


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	for i in PlayerData.PlayerHP:
		var NewHeart = Sprite2D.new()
		NewHeart.texture = $PlayerHP.texture
		NewHeart.hframes = $PlayerHP.hframes
		$PlayerHP.add_child(NewHeart)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta) -> void:
	$CoinCount.text = str(PlayerData.Coin)
	display_heart()


func display_heart():
	for heart in $PlayerHP.get_children():
		var Index = heart.get_index()
		var x = (Index % HEART_ROW_SIZE) * HEART_OFFSET
		var y = (Index / HEART_ROW_SIZE) * HEART_OFFSET
		heart.position = Vector2(x, y)
		
		var LatestHeart = floor(PlayerData.PlayerHP)
		if Index > LatestHeart:
			heart.frame = 0
		if Index == LatestHeart:
			heart.frame = (PlayerData.PlayerHP - LatestHeart) * 4
		if Index < LatestHeart:
			heart.frame = 4
			
